﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace MTRC.ODMS.Web.SOM.CodeSetup
{
    public partial class FireLinkRestoreEntry : BasePage_SOM
    {
        protected override string MenuID
        {
            get
            {
                return "M140302";
            }
        }

        /// <summary>
        /// page load event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitPage();
                SetLanguage();
            }
            else
            {
                this.IsServerChanged = true;
            }
        }

        /// <summary>
        /// init page event
        /// </summary>
        protected void InitPage()
        {
            BindListControl(this.ddlLine, "LineId", "LineCode", DAL.SOM.CodeSetup.CDLine.GetAllODMLines(Convert.ToInt32(LangCode)), true, "-1");

            BindListControl(this.ddlBelongsToLineGroup, "LineId", "LineCode", DAL.SOM.CodeSetup.CDLine.GetSOMFlagGroup(Convert.ToInt32(LangCode)), true, "-1");

            if (Request.QueryString["SOMLineID"] != null && Request.QueryString["action"].ToUpper() == "EDIT")
            {
                DataTable DTable = DAL.SOM.CodeSetup.CDLine.GetLineByLineId(Convert.ToInt32(Request.QueryString["SOMLineID"]), this.LangCode);

                if (DTable == null || DTable.Rows.Count == 0)
                {
                    return;
                }
                this.ddlLine.SelectedValue = DTable.Rows[0]["Line"].ToString();
                this.txtCode1.Text = this.HiddenOriginalCode1.Value = DTable.Rows[0]["Code1"].ToString();
                this.txtCode2.Text = DTable.Rows[0]["Code2"].ToString();
                this.txtCode3.Text = DTable.Rows[0]["Code3"].ToString();
                this.txtDescription1.Text = DTable.Rows[0]["Description1"].ToString();
                this.txtDescription2.Text = DTable.Rows[0]["Description2"].ToString();
                this.txtDescription3.Text = DTable.Rows[0]["Description3"].ToString();
                this.txtSequence.Text = DTable.Rows[0]["Sequence"].ToString();

                this.ddlBelongsToLineGroup.SelectedValue = DTable.Rows[0]["LineGroupID"].ToString();
                this.cbxLineGroup.Checked = SetIsChecked(DTable.Rows[0]["FlagGroup"].ToString());
                this.cbxShowInFireLinkPanel.Checked = SetIsChecked(DTable.Rows[0]["FlagFirePanel"].ToString());
                this.txtFontColor.Text = DTable.Rows[0]["FirePanelFontColor"].ToString();
                this.txtBackColor.Text = DTable.Rows[0]["FirePanelBGColor"].ToString();
                this.cbxObsolete.Checked = SetIsChecked(DTable.Rows[0]["FlagObsolete"].ToString());
                this.HiddenSOMLineId.Value = Request.QueryString["SOMLineID"];

                if (cbxLineGroup.Checked)
                {
                    this.ddlBelongsToLineGroup.Enabled = false;
                }
                else
                {
                    this.ddlBelongsToLineGroup.Enabled = true;
                }

            }
            else if (Request.QueryString["action"] != null && Request.QueryString["action"].ToUpper() == "NEW")
            {
                this.HiddenSOMLineId.Value = "0";
            }
            else
            {
                Response.Redirect("LineEnquiry.aspx");
            }
        }

        /// <summary>
        /// Set language
        /// </summary>
        protected void SetLanguage()
        {
            this.lblMenuName.Text = GetLabel("SOM_LineEntry", false);
            this.lblLine.Text = this.GetLabel("SOM_LineLine", true);
            this.lblCode1.Text = this.GetLabel("SOM_LineCode1", true);
            this.lblCode2.Text = this.GetLabel("SOM_LineCode2", true);
            this.lblCode3.Text = this.GetLabel("SOM_LineCode3", true);
            this.lblDescription1.Text = this.GetLabel("SOM_LineDescription1", true);
            this.lblDescription2.Text = this.GetLabel("SOM_LineDescription2", true);
            this.lblDescription3.Text = this.GetLabel("SOM_LineDescription3", true);
            this.lblSequence.Text = this.GetLabel("SOM_LineSequence", true);
            this.lblBelongsToLineGroup.Text = this.GetLabel("SOM_LineBelongtoLineGroup", true);
            this.lblLineGroup.Text = this.GetLabel("SOM_LineLineGroup", true);
            this.lblFireLinkStatus.Text = this.GetLabel("SOM_LineLineStatus", true);
            this.lblFontColor.Text = this.GetLabel("SOM_LineFontColor", true);
            this.lblBackColor.Text = this.GetLabel("SOM_LineBackColor", true);
            this.lblObsolete.Text = this.GetLabel("SOM_LineObsolete", true);
            this.btnSave.Text = this.GetLabel("SOM_LineSave", false);
            this.btnSaveAndReturn.Text = this.GetLabel("SOM_LineSaveAndReturn", false);
            this.btnCancel.Text = this.GetLabel("SOM_LineCancel", false);
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.ChangeLanguage += new ChangeLanguageEventHandler(LineEntry_ChangeLanguage);
        }

        private void LineEntry_ChangeLanguage(object sender, EventArgs e)
        {
            SetLanguage();
            if (IsPostBack)  
            {
                InitPage();
            }
        }

        /// <summary>
        /// save data function
        /// </summary>
        /// <param name="actionType"></param>
        protected void SaveData(int actionType)
        {
            //default is failure state
            int mResult = 0;
            int outPutLineId = 0;

            if (string.IsNullOrEmpty(this.txtCode1.Text.Trim()))
            {
                this.PAlert(this.GetMessage("SOM_LineCodeNotNull"));
                return;
            }
            else
            {
                if (!this.txtCode1.Text.Trim().Equals(this.HiddenOriginalCode1.Value, StringComparison.CurrentCultureIgnoreCase))
                {
                    int result = DAL.SOM.CodeSetup.CDLine.GetCodeByLineId(this.txtCode1.Text.Trim());
                    if (result >= 1)
                    {
                        this.PAlert(this.GetMessage("SOM_DuplicateCode1"));
                        return;
                    }
                }
            }

            if (string.IsNullOrEmpty(this.txtDescription1.Text.Trim()))
            {
                this.PAlert(this.GetMessage("SOM_LineDescriptionNotNull"));
                return;
            }
            if (string.IsNullOrEmpty(this.txtSequence.Text.Trim()))
            {
                this.PAlert(this.GetMessage("SOM_LineSeqNo"));
                return;
            }
            try
            {
                mResult = DAL.SOM.CodeSetup.CDLine.UpdateSOMCDLine(Convert.ToInt32(this.HiddenSOMLineId.Value), Convert.ToInt32(this.ddlLine.SelectedValue),
                                this.txtCode1.Text.Trim(), this.txtCode3.Text.Trim(), this.txtCode3.Text.Trim(),
                                this.txtDescription1.Text.Trim(), this.txtDescription2.Text.Trim(), this.txtDescription3.Text.Trim(),
                                Convert.ToInt32(this.txtSequence.Text), Convert.ToInt32(this.ddlBelongsToLineGroup.SelectedValue), this.cbxLineGroup.Checked,
                                this.cbxShowInFireLinkPanel.Checked, this.txtFontColor.Text.Trim(), this.txtBackColor.Text.Trim(),
                                this.cbxObsolete.Checked, this.ODMUser.ID, this.ODMUser.ID, ref outPutLineId);

                if (mResult == 1)
                {
                    if (actionType == 1)
                    {
                        this.HiddenSOMLineId.Value = outPutLineId.ToString();
                        this.HiddenOriginalCode1.Value = this.txtCode1.Text.Trim();
                        this.PAlert(this.GetMessage("SaveSuccessfully"));
                    }
                    if (actionType == 2)
                    {
                        Response.Redirect("LineEnquiry.aspx");
                    }
                }
                else
                {
                    this.PAlert(this.GetMessage("SaveUnsuccessfully"));
                }

            }
            catch (Exception ex)
            {
                this.PAlert(ex.Message);
            }
        }

        /// <summary>
        /// cbx line group event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void cbxLineGroup_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cbxLineGroup.Checked)
            {
                this.ddlBelongsToLineGroup.Enabled = false;
                this.ddlBelongsToLineGroup.SelectedValue = "-1";
            }
            else
            {
                this.ddlBelongsToLineGroup.Enabled = true;
            }
        }

        /// <summary>
        ///the dropdownlist line event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlLine_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = MTRC.ODMS.DAL.SOM.CodeSetup.CDLine.GetODMCDLineByLineId(Convert.ToInt32(this.ddlLine.SelectedValue));
            if (dt == null || dt.Rows.Count == 0)
            {
                this.txtCode1.Text = string.Empty;
                this.txtCode2.Text = string.Empty;
                this.txtCode3.Text = string.Empty;
                this.txtDescription1.Text = string.Empty;
                this.txtDescription2.Text = string.Empty;
                this.txtDescription3.Text = string.Empty;
                return;
            }

            this.txtCode1.Text = dt.Rows[0]["Code1"].ToString();
            this.txtCode2.Text = dt.Rows[0]["Code2"].ToString();
            this.txtCode3.Text = dt.Rows[0]["Code3"].ToString();
            this.txtDescription1.Text = dt.Rows[0]["Description1"].ToString();
            this.txtDescription2.Text = dt.Rows[0]["Description2"].ToString();
            this.txtDescription3.Text = dt.Rows[0]["Description3"].ToString();
        }

        /// <summary>
        /// Set checked value
        /// </summary>
        /// <param name="isChecked"></param>
        /// <returns></returns>
        protected bool SetIsChecked(string isChecked)
        {
            return isChecked == "True" ? true : false;
        }

        /// <summary>
        /// Save event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.SaveData(1);
        }

        /// <summary>
        /// Save and return page
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveAndReturn_Click(object sender, EventArgs e)
        {
            this.SaveData(2);
        }
    }
}